<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Session;
use think\captcha\Captcha;
class Login extends Controller
{
    public function index()
    {
        // 关闭模板布局
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    // 生成验证码
    public function verify(){
        $config  = [
            'fontSize' => 16,
            'length' => 4,
            'useCurve' => false,
            'useNoise' => false,
            'codeSet' => '023456789'
        ];
        $captcha = new Captcha($config);
        return $captcha->entry();
    }
    public function login(){
        $request=new request();
        $code = $request->post("captcha");
        $captcha = new Captcha();
        // 验证验证码
        if(!$captcha->check($code))
        {
            return $this->error('验证码输入错误');
        }else{
            $username = $request->post("username");
            $password = $request->post("password");
            // 根据姓名判断有没此账号
            $data=Db::table('admin')->where('adxm',$username)->find();
            if(empty($data)){
                // 根据手机判断有没此账号
                $data=Db::table('admin')->where('adphone',$username)->find();
                if(empty($data)){
                    return $this->error("无此账号请重新输入",'index');
                }     
            }
            // 有账号验证密码
            if(!empty($data)){
                if($password==$data['adpd']){
                    Session::set('adid',$data['adid']);
                    Session::set('adxm',$username);
                    Session::set('adgrade',$data['adgrade']);
                    Session::set('adimg',$data['adimg']);
                    return $this->success("登录成功",'admin/index/index');
                }else{
                    return $this->error("密码错误请重新输入",'index');
                }

            }           
        }
    }
    // 退出登录
    public function outlogin()
    {
        Session::delete('adid');
        Session::delete('adxm');
        Session::delete('adgrade');
        Session::delete('adimg');
        return $this->fetch('login/index');
    }
}
